<?php
namespace Home\Controller;
use Think\Controller;
include 'phpQuery/phpQuery.php';
class ZhihuController extends Controller {
    public function index(){
      $crawler = $this->crawler();
      $newCra = $this->arrayCom($crawler);
      $this->assign('newCra',$newCra);
      // $this->p($crawler);
      $this->display();
    }

    public function arrayCom($data){
        $newArr = array();
        /**
         * 简单的逻辑想了那么久
         */
        for ($i=0; $i < 10 ; $i++) {
          $newArr["$i"]['title'] = $data['title']["$i"];
          $newArr["$i"]['praise'] = $data['praise']["$i"];
          $newArr["$i"]['author'] = $data['author']["$i"];
          $newArr["$i"]['abstract'] = $data['abstract']["$i"];
          $newArr["$i"]['content'] = $data['content']["$i"];
        }
        return $newArr;
    }

    public function p($data){
      echo '<pre>';
      print_r($data);
      echo '</pre>';
    }

    public function crawler(){
        /**
         * 官网 http://code.google.com/p/phpquery/
         * 爬知乎PHP专栏每条帖子(后期输入专栏爬)<前十条>
         */
        \phpQuery::newDocumentFile('https://www.zhihu.com/search?type=content&q=php');
        $artlist = pq(".title");
        $praise = pq(".zm-item-vote-count");
        $author = pq(".author-line");
        $abstract = pq(".author-line");
        $content = pq(".zm-item-vote-info");

        //不知为何要严格定义这个数组,否则无法赋值
        $data = array();
        foreach($artlist as $k=>$li){
          $data['title']["$k"] = pq($li)->html();
        }
        $even = 0;
        foreach ($praise as $k => $v) {
          if ($k%2!=0) {
            continue;
          }
          $data['praise']["$even"] = pq($v)->html();
          $even++;
        }

        foreach ($author as $k => $v) {
          if (pq($v)->html() == '匿名用户') {
            $data['author']["$k"] = '匿名用户';
          }else{
            $data['author']["$k"] = pq($v)->find('span')->find('a')->html();
          }
          // $data['author']["$k"] = pq($v)->html();
        }

        foreach ($abstract as $k => $v) {
          if((pq($v)->find('span:eq(1)')->html())==""){
              $data['abstract']["$k"] = ' ';
          }else{
              $data['abstract']["$k"] = pq($v)->find('span:eq(0)')->html();
          }
        }

        foreach ($content as $k => $v) {
          $data['content']["$k"] = pq($v)->next()->html();
        }
        /**
         * 测试输出
         */
       //  p($data);

       return $data;

        /**
         * 解析XML文档
         */
        // include 'phpQuery/phpQuery.php';
        // phpQuery::newDocumentFile('test.xml');
        // echo pq('contact > age:eq(0)');
    }
}
?>
